setTimeout(function(){ 
	$("#name").focus();
	$("#name").rules("add",{
		remote: {
			url: 'qz/channel/checkName',
			type: "get",
			data: {"id":getQueryString("id")},
			dataType: "JSON", 
			dataFilter:function(data,type){						 
				return data=="false"?false:true;
			}
		}, 		
		messages: {remote: "该机构已存在"}
	});
} , parseInt(Math.random() * 1000));

setTimeout(function(){
	$("#code").rules("add",{
		remote: {
			url: 'qz/channel/checkCode',
			type: "get",
			data: {"id":getQueryString("id")},
			dataType: "JSON", 
			dataFilter:function(data,type){		 
				return data=="false"?false:true;
			}
		}, 		
		messages: {remote: "该编码已存在"}
	}); 
} , parseInt(Math.random() * 1000));

var app = angular.module('qzchannelApp', []);
var channelid='1';
var chcode='1';
var chname='1';
app.controller('qzchannelController', function($scope, $http) {
	var busid={};
	$scope.business = [];
	$scope.view=function(){
		
		
		
		$http.get(timestamp('qz/channel/getRelateBusi/'+getQueryString("id"))).success(function(response){
			$scope.relateBusiList=response.data;			
			$scope.relateBusiList.forEach(function(item){
				if(item.channelId != null){
					$scope.business.push(item.businessId);			 
				}
				
			});
			 
	    });
		
		$http.get(timestamp("qz/channel/get/" + getQueryString("id"))).success(function (response) {
			$scope.qzmsg = response.data;
//			console.log($scope.qzmsg);
			$http.get('qz/business/getAll?status=2').success(function(response){
				$scope.businessList=response.data;
				//console.log($scope.businessList);
				var param = {
						name: $scope.businessList,
						qzname: $scope.qzmsg.businessname
				}
				//console.log(param);
//				$http.post('qz/channel/getBybusName/', param).success(function(response){
//					$scope.resname = response.data;
//				});
//				console.log($scope.qzmsg.businessname);
	        });
			$http.get('qz/channel/getAll/').success(function(response){
				$scope.menuList=response.data;
	        });
		});
	}
	
	$scope.view();
	$scope.refreshData=function(){
		$scope.view();
	}
	
	 
//	$scope.busclick=function(msg) {
//		var id = [];
//		$('input[name="bus"]:checked').each(function(){ 
//			id.push($(this).val()); 
//		});
//		busid = id;
//		alert(busid);
//	}
	
		
    // toggle selection for a given interProps by id
    $scope.toggleSelection = function toggleSelection(bId) {
    	var idx = $scope.business.indexOf(bId);
    	// is currently selected
   
    	if (idx > -1) {
    		$scope.business.splice(idx, 1);
    	}else {
    		// is newly selected
    		$scope.business.push(bId);
    	}     	
    };
	
	$scope.vername=function(name){
		$http.get('qz/channel/getAll/', {params: {name:name}}).success(function(response){
			$scope.nameList=response.data;
//			console.log($scope.nameList);
			if($scope.nameList.length == 0) {
				channelid = '';
			}else {
				chname = '2';
				channelid = $scope.nameList[0].channelId;
			}
		});		
	}
	
	$scope.vercode=function(code){
		$http.get('qz/channel/getAll/', {params: {code:code}}).success(function(response){
			$scope.codeList=response.data;
			if($scope.codeList.length == 0) {
				channelid = '1';
			}else {
				chcode = $scope.codeList[0].code;
				channelid = $scope.codeList[0].channelId;
			}
		});
	}
	
//	$scope.chSave=function(){
//		$http.get('qz/channel/getAll/', {params: {name:$scope.qzmsg.name, code:$scope.qzmsg.code}}).success(function(response){
//			$scope.nameList=response.data;
//			console.log($scope.nameList);
//			if($scope.nameList.length == 0) {
//				alert(2);
//				reslist = '';
//				channelid = '';
//				code = '';
//			}else {
//				reslist = $scope.nameList[0].name;
//				channelid = $scope.nameList[0].channelId;
//				code = $scope.nameList[0].code;
//			}
//			if(code == '' && $scope.nameList[0].name != '') {
//				if($scope.qzmsg.name == reslist && channelid == $location.search().id) {
//					alert(1);
//					if($("#myForm").valid()){
//						var param = {
//								id  : $location.search().id,
//								name: $scope.qzmsg.name,
//								code: $scope.qzmsg.code,
//								memo: $scope.qzmsg.memo,
//								busid: busid
//						}
//						$http.post('qz/channel/modify/', param).success(function(){
//							window.location.href = "manage/channel/list.jsp";
//						});
//					}
//				}else if($scope.qzmsg.name != reslist && channelid == '') {
//					alert(4);
//					if($("#myForm").valid()){
//						var param = {
//								id  : $location.search().id,
//								name: $scope.qzmsg.name,
//								code: $scope.qzmsg.code,
//								memo: $scope.qzmsg.memo,
//								busid: busid
//						}
//						$http.post('qz/channel/modify/', param).success(function(){
//							window.location.href = "manage/channel/list.jsp";
//						});
//					}
//				}
//				
//			}
//			
//        });
//	}
	
	$scope.chSave=function(){		
		if(channelid == getQueryString("id") && chname != '1' && chname != '2') {
//			alert("channelid=" + channelid);
			 
			 
			if($("#myForm").valid()){
				var param = {
						id  : getQueryString("id"),
						name: $scope.qzmsg.name,
						code: $scope.qzmsg.code,
						status: $scope.qzmsg.status,
						memo: $scope.qzmsg.memo,
						busid: $scope.business.join(",")
				}
				$http.post('qz/channel/modify/', param).success(function(){
					redirectUrl("manage/channel/","list.jsp");
				});
			}
		}else if(channelid == '') {
//			alert("channelid=" + channelid);
			 
			if($("#myForm").valid()){
				var param = {
						id  : getQueryString("id"),
						name: $scope.qzmsg.name,
						code: $scope.qzmsg.code,
						status: $scope.qzmsg.status,
						memo: $scope.qzmsg.memo,
						busid: $scope.business.join(",")
				}
				$http.post('qz/channel/modify/', param).success(function(){
					redirectUrl("manage/channel/","list.jsp");
				});
			}
		}else if(channelid == '1' && chname == '1') {
//			alert($scope.qzmsg.code);
//			alert(chcode);
			 
			if($("#myForm").valid()){
				var param = {
						id  : getQueryString("id"),
						name: $scope.qzmsg.name,
						code: $scope.qzmsg.code,
						status: $scope.qzmsg.status,
						memo: $scope.qzmsg.memo,
						busid: $scope.business.join(",")
				}
				$http.post('qz/channel/modify/', param).success(function(){
					redirectUrl("manage/channel/","list.jsp");
				});
			}
		}else if(channelid == getQueryString("id") && chname == '1') {
			 
			if($("#myForm").valid()){
				var param = {
						id  : getQueryString("id"),
						name: $scope.qzmsg.name,
						code: $scope.qzmsg.code,
						status: $scope.qzmsg.status,
						memo: $scope.qzmsg.memo,
						busid: $scope.business.join(",")
				}
				$http.post('qz/channel/modify/', param).success(function(){
					redirectUrl("manage/channel/","list.jsp");
				});
			}
		}
		else {
//			alert("channelid=" + channelid);
//			alert("chname=" + chname);
//			alert(4);
		}
	}
	
	
	$scope.chReset=function(){
		$scope.view();
	}
	
	$scope.chCancle=function(){
		redirectUrl("manage/channel/","list.jsp");
	}
});
